package cn.highedu.coolsharkhub.controller;

import cn.highedu.coolsharkhub.pojo.vo.BannerHomepageVO;
import cn.highedu.coolsharkhub.pojo.vo.CategoryVO;
import cn.highedu.coolsharkhub.pojo.vo.ProductHomepageVO;
import cn.highedu.coolsharkhub.service.BannerService;
import cn.highedu.coolsharkhub.service.CategoryService;
import cn.highedu.coolsharkhub.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HomeController {

    private Logger logger = LoggerFactory.getLogger(HomeController.class);

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BannerService  bannerService;

    @Autowired
    private ProductService productService;

    /**
     * 查询首页商品类别
     * @return 首页商品类别(Spring MVC会以JSON格式返回)
     */
    @GetMapping("/home/categories")
    public List<CategoryVO> findCategories() {
        logger.info("查询首页商品类别");
        return categoryService.findHomepageCategories();
    }

    /**
     * 查询所有轮播图
     * @return 轮播图列表
     */
    @GetMapping("/home/banners")
    public List<BannerHomepageVO> findBanners() {
        logger.info("查询所有轮播图");
        return bannerService.findAllForHomepage();
    }

    /**
     * 根据商品分类id查询商品列表
     * @param categoryId 商品分类id
     * @return 商品列表
     */
    @GetMapping("/home/categories/{categoryId}/products")
    public List<ProductHomepageVO> findByCategoryIdForHomepage(@PathVariable Integer categoryId) {
        logger.info("根据商品分类id查询商品列表");
        return productService.findByCategoryIdForHomepage(categoryId);
    }

}
